import {Component} from '@angular/core';

import {NavController, NavParams} from 'ionic-angular';
import {Http} from '@angular/http';

import {SectionPage} from '../section/section';
import {parser} from '../../engine/findengine'
import {Storage} from '@ionic/storage';
// import {Md5} from 'ts-md5/dist/md5';

@Component({
  selector: 'page-book',
  templateUrl: 'book.html'
})
export class BookPage {
  selectedItem: any;
  book: { name: string, logo: string, sections: Array<any> };
  color: string;
  isFavour:boolean;
  items: Array<{ title: string, note: string, logo: string, url: string }>;

  constructor(public navCtrl: NavController, public navParams: NavParams, public http: Http, public storage: Storage) {
    this.selectedItem = navParams.get('item');
    let url = this.selectedItem.url;
    this.items = [];
    this.color = 'dark';
    this.isFavour = false;
    this.book = {name: null, logo: null, sections: []};
    http.get(url)
      .subscribe(data => {
        this.book = JSON.parse(parser.book_parse(data.url, data.text()));
        this.storage.ready().then(() => {
          this.storage.get('favour').then((val) => {
            let favour;
            if(val!=null){
              favour = val;
              if(favour.filter(v => v.url === this.selectedItem.url).length !== 0){
                this.color='danger';
                this.isFavour = true;
              }
            }
          });
          this.storage.get('history').then((val) => {
            let history;
            let flag = false;
            if (val == null) {
              history = [];
              flag = true;
            } else {
              history = val;
            }
            let url = this.selectedItem.url;
            if (history.filter(v => v.url === url).length === 0) {
              history.push(this.selectedItem);
              flag = true;
            }
            while(history.length>10){
              history.shift();
            }
            if (flag) {
              storage.set('history', history);
            }
          })
        });

      }, error => {
        console.log(JSON.stringify(error.json()));
      });// end http.get
  } // end constructor

  itemTapped(event, item, index) {
    this.navCtrl.push(SectionPage, {
      item: item,
      index: index,
      book: this.book
    });
  }

  iconTapped(event) {
    this.storage.ready().then(() => {
      this.storage.get('favour').then((val) => {
        let favour;
        if(val == null){
          favour = [];
        }else{
          favour = val;
        }
        if(this.isFavour){
          favour = favour.filter(v => v.url !== this.selectedItem.url);
          this.storage.set('favour',favour);
          this.color = 'dark';
        }else{
          if(favour.filter(v => v.url === this.selectedItem.url).length===0){
            favour.push(this.selectedItem);
          }
          this.storage.set('favour',favour);
          this.color = 'danger';
        }
        this.isFavour = !this.isFavour;
      });
    });
  }
}
